import os


def get_pj_path() -> str:
    """
    获取当前项目目录
    :return: str
    """
    return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


def get_config_file_path(env: str)-> str:
    return f"{get_pj_path()}/common/config/conf/{env}.ini"


def init_settings():
    from common.config.settings import Settings
    from common.config.settings import MySQLConfig, RedisConfig, JWTConfig, IdWorkerConfig, RabbitMQConfig, ESConfig
    env = os.environ.get("ENV", "local").lower()
    pj_path = get_pj_path()
    if env == "local":
        from common.config.conf import local as cfg
    elif env == "dev":
        from common.config.conf import dev as cfg
    elif env == "test":
        from common.config.conf import test as cfg
    elif env == "uat":
        from common.config.conf import uat as cfg
    elif env == "prod":
        from common.config.conf import prod as cfg
    else:
        raise ValueError("环境错误")

    return Settings(
        env=env,
        pj_path=pj_path,
        log_level=cfg.LOG.get("log_level"),
        log_path=cfg.LOG.get("log_path"),
        mysql_config=MySQLConfig(**cfg.DATABASE.get("default")),
        redis_config=RedisConfig(**cfg.REDIS.get("default")),
        static_root=cfg.STATIC.get("static_root"),
        jwt_config=JWTConfig(**cfg.JWT),
        id_worker_config=IdWorkerConfig(
            worker_id_bit=cfg.ID_WORKER_WORKER_ID_BITS,
            data_center_id_bit=cfg.ID_WORKER_DATACENTER_ID_BITS,
            sys_starttimestamp=cfg.ID_WORKER_STARTTIMESTAMP,
            sequence_bit=cfg.ID_WORKER_SEQUENCE_BITS
        ),
        rabbitmq_config=RabbitMQConfig(**cfg.RABBITMQ_CONFIG),
        es_config=ESConfig(**cfg.ES_CONFIG)
    )
